package listdemo;

import java.util.Stack;

public class MySingleList {

    static class ListNode {
        private int val;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;//代表单链表头结点的引用

    public void creatList() {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        ListNode listNode5 = new ListNode(5);

        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;

        this.head = listNode1;
    }

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
//        if (head == null) {
//            head = node;
//        } else {
//            node.next = head;
//            this.head = node;
//        }
        node.next = this.head;
        head = node;
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
        } else {
            ListNode cur = head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    //任意位置插入
    public void addIndex(int index, int data) throws MySingleListIndexOutOfException{
        checkIndexAdd(index);
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = findIndexSubOne(index);
        node.next = cur.next;
        cur.next = node;
    }

    /**
     * 找到index-1位置的节点
     * @param：index
     * @return 该节点的地址
     */
    private ListNode findIndexSubOne(int index) {
        ListNode cur = head;
        while (index-1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    private void checkIndexAdd(int index) {
        if (index < 0 || index > size()) {
            throw new MySingleListIndexOutOfException("任意位置插入时，index不合法");
        }
    }

    //查找是否包含关键词key是否在单链表中
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键词key的节点
    public void remove(int key) {
        if (head == null) {
            System.out.println("链表为空，不能进行删除！");
            return;
        }

        if (this.head.val == key) {
            this.head = this.head.next;
            return;
        }

        ListNode cur = this.head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                ListNode del = cur.next;
                cur.next = del.next;
                return;
            }
            cur = cur.next;
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if (this.head == null) {
            return;
        }

        ListNode cur = this.head.next;
        ListNode prev = this.head;
        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (this.head.val == key) {
            head = head.next;
        }
    }

    public ListNode removeElements(int val) {
        if(head == null) {
            return head;
        }
        ListNode cur = head;
        while(cur.next != null) {
            if (cur.next.val == val) {
                cur.next = cur.next.next;
            } else {
                cur = cur.next;
            }
        }
        if(head.val == val) {
            head = head.next;
        }
        return head;
    }

    //得到单链表的长度
    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void display() {
        ListNode cur = this.head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear() {
        //this.head = null;
        ListNode cur = this.head;
        ListNode curNext = null;
        while (cur != null) {
            curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
        head = null;
    }

    /**
     * 递归倒序打印
     * @param head
     */
    public void display1(ListNode head) {
        if (head == null) {
            return;
        }
//        if (head.next == null) {
//            System.out.println(head.val);
//            return;
//        }
        display1(head.next);
        System.out.print(head.val);
    }

    /**
     * 栈倒序打印
     * @param head
     */
    public void display2() {
        if (head == null) {
            return;
        }
        Stack<ListNode> stack = new Stack<>();
        ListNode cur = head;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        while (!stack.empty()) {
            ListNode p = stack.pop();
            System.out.println(p.val);
        }
    }
}
